package com.ldxx.mustwinlottery.presenter.impl;


import com.ldxx.mustwinlottery.app.LotteryApplication;
import com.ldxx.mustwinlottery.base.IBaseRequestCallBack;
import com.ldxx.mustwinlottery.model.IssueModel;
import com.ldxx.mustwinlottery.model.bean.IssueBean;
import com.ldxx.mustwinlottery.model.impl.IssueModelImpl;
import com.ldxx.mustwinlottery.presenter.IssueInfoPresenter;
import com.ldxx.mustwinlottery.ui.view.SplashView;

import java.util.List;

import io.realm.Realm;
import io.realm.RealmResults;

/**
 *
 * Created by ldxx on 16/8/29.
 */

public class IssueInfoPresenterImpl implements IssueInfoPresenter, IBaseRequestCallBack<List<IssueBean>> {
    //private static final String TAG = "IssueInfoPresenterImpl";
    private SplashView view;
    private IssueModel issueModel;

    public IssueInfoPresenterImpl(SplashView view) {
        this.view = view;
        issueModel = new IssueModelImpl();
    }

    @Override
    public void getIssueInfos() {
        issueModel.getIssueInfos(this);
    }

    @Override
    public void requestError(Throwable e) {
        view.afterGetIssueInfo();
    }

    @Override
    public void requestComplete() {

    }

    @Override
    public void requestSuccess(List<IssueBean> issue) {
        Realm realm = LotteryApplication.getRealm();
        RealmResults<IssueBean> list = realm.where(IssueBean.class).findAll();

        realm.beginTransaction();
        list.deleteAllFromRealm();
        realm.copyToRealm(issue);
        realm.commitTransaction();
        view.afterGetIssueInfo();
    }
}
